import {
    inputchange,
    dateFormat
} from '../../../utils/util'
import {
    queryUserInfo,
    testingDanger
} from '../../api/insurance'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: '',
        outDangerDate: '',
        userInfoList: [],
        total: 0,
        hasnextpage: true,
        pageNum: 1,
        info: {
            id: '',
            phone: '',
            name: '',
            comboname: '',
            createtime: '',
            idCardNo: ''
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options, 'options')
        this.setData({
            outDangerDate: options.outDangerDate
        })
        this.onInputChange('', true)
    },
    onInputChangec(e) {
        const iptValue = e.detail.value
        this.setData({
          value: iptValue,
          userInfoList: [],
          hasnextpage:true,
          pageNum: 1
        })
        this.onInputChange()
        // this.setData({
        //     userInfoList: [],
        //     hasnextpage: true,
        //     pageNum: 1
        // })
        // queryUserInfo({
        //     insureDay: '',
        //     outDangerUserName: e.detail.value,
        //     insureCompanyType: 'insure_type_rrb',
        //     isQueryOutDanger: 1,
        //     pageSize: 10,
        //     pageNum: this.data.pageNum
        // }).then(res => {
        //     console.log(res, 'hysudigdgidsiug');
        //     res.list.forEach((item, index) => {
        //         res.list[index].createTime = dateFormat('YYYY-mm-dd HH:MM', new Date(item.createTime.replace(/-/g, '/')))
        //     })
        //     this.setData({
        //         userInfoList: [...res.list, ...this.data.userInfoList],
        //         total: res.totalCount,
        //         hasnextpage: res.hasNextPage
        //     })
        // })
    },
    onInputChange() {
        // if (!skip) {
        //     inputchange(event, this)
        // }
        console.log(1111, this.data.value);
        queryUserInfo({
            insureDay: '',
            outDangerUserName: this.data.value,
            insureCompanyType: 'insure_type_rrb',
            isQueryOutDanger: 1,
            pageSize: 10,
            pageNum: this.data.pageNum
        }).then(res => {
            console.log(res, 'hysudigdgidsiug');
            res.list.forEach((item, index) => {
                res.list[index].createTime = dateFormat('YYYY-mm-dd HH:MM', new Date(item.createTime.replace(/-/g, '/')))
            })
            this.setData({
                userInfoList: [ ...this.data.userInfoList, ...res.list],
                total: res.totalCount,
                hasnextpage: res.hasNextPage
            })
        })
    },
    async onInsuranceSelect(event) {
        const {
            id,
            user,
            phone,
            name,
            comboname,
            card,
            createtime
        } = event.currentTarget.dataset
        // console.log(event.currentTarget.dataset, 'currentTarget')
        const result = await testingDanger({
            recordId: id,
            idCardNo: card
        })
        console.log(result, 'result')
        this.setData({
            ['info.id']: id || '',
            ['info.user']: user || '',
            ['info.phone']: phone || '',
            ['info.name']: name,
            ['info.comboname']: comboname,
            ['info.idCardNo']: card,
            ['info.createtime']: createtime,
        })
        wx.navigateTo({
            url: `/packageInsurance/pages/insurance-select/insurance-select?id=${this.data.info.id}&user=${this.data.info.user}&phone=${this.data.info.phone}&name=${this.data.info.name}&comboname=${this.data.info.comboname}&phone=${this.data.info.phone}&createtime=${this.data.info.createtime}&idCardNo=${this.data.info.idCardNo}`,
        })
        // const eventChannel = this.getOpenerEventChannel()
        // eventChannel.emit('someEvent', this.data.info)
        // wx.navigateBack(-1)
    },
    onReachBottom() {
        console.log('下一页')
        if (this.data.hasnextpage) {
            this.setData({
                pageNum: this.data.pageNum + 1
            })
            this.onInputChange()
            // this.onInputChange('', true)
        } else {
            wx.showToast({
                title: '没有更多数据了',
                icon: 'none'
            })
        }
    },
})